package com.twotwoshop.service.user.service.Impl;



import com.twotwoshop.commom.redis.RedisConstants;
import com.twotwoshop.domin.common.dtos.ResponseResult;
import com.twotwoshop.domin.common.enums.AppHttpCodeEnum;
import com.twotwoshop.service.user.service.UserStatsService;
import com.twotwoshop.util.thread.AppThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
@RequiredArgsConstructor
public class UserStatsServiceImpl implements UserStatsService {

    private final StringRedisTemplate stringRedisTemplate;


    /**
     * 查询用户基本信息
     *
     * @return
     */
    @Override
    public ResponseResult search() {


        //1.获取用户id
        Integer id = AppThreadLocalUtil.getUser().getId();

        Map<String,String> map = new HashMap<>();

        //2.从Redis中获取用户浏览历史个数
        Long viewsHistoryCount = stringRedisTemplate.opsForZSet().size(RedisConstants.GOODS_VIEWS_HISTORY_KEY + id.toString());

        if (viewsHistoryCount <= 0) {
            map.put("history", String.valueOf(0));
        }


        //3.获取用户收藏个数
        Long favouriteCount = stringRedisTemplate.opsForZSet().size(RedisConstants.GOODS_FAVOURITE_HISTORY_KEY + id.toString());

        if (favouriteCount <= 0) {
            map.put("favorite",String.valueOf(0));
        }


        //3.获取用户关注个数
        Long FollowCount = stringRedisTemplate.opsForZSet().size(RedisConstants.USER_FOLLOW_KEY + id.toString());
        if (FollowCount <= 0) {
            map.put("follow",String.valueOf(0));
        }

        //4.获取用户粉丝个数
        Long FanCount = stringRedisTemplate.opsForZSet().size(RedisConstants.USER_FAN_KEY + id.toString());
        if (FollowCount <= 0) {
            map.put("fans",String.valueOf(0));
        }



        map.put("history",viewsHistoryCount.toString());
        map.put("favorite",favouriteCount.toString());
        map.put("following",FollowCount.toString());
        map.put("fans",FanCount.toString());

        return ResponseResult.okResult(map);
    }
}




































